import { Modal } from 'antd';
import { WeaTools } from 'ecCom';

const getKey = WeaTools.getKey;
import isEmpty from 'lodash/isEmpty';
import isArray from 'lodash/isArray';

export const openWindow = (url) => {
  const width = screen.availWidth - 10;
  const height = screen.availHeight - 50;
  let szFeatures = 'top=0,';
  szFeatures += 'left=0,';
  szFeatures += `width=${width},`;
  szFeatures += `height=${height},`;
  szFeatures += 'directories=no,';
  szFeatures += 'status=yes,toolbar=no,location=no,';
  szFeatures += 'menubar=no,';
  szFeatures += 'scrollbars=yes,';
  szFeatures += 'resizable=yes';
  window.open(url, '', szFeatures);
};

export const openWindowSingle = (url) => {
  const width = screen.availWidth - 10;
  const height = screen.availHeight - 50;
  let szFeatures = 'top=0,';
  szFeatures += 'left=0,';
  szFeatures += `width=${width},`;
  szFeatures += `height=${height},`;
  szFeatures += 'directories=no,';
  szFeatures += 'status=yes,toolbar=no,location=no,';
  szFeatures += 'menubar=no,';
  szFeatures += 'scrollbars=yes,';
  szFeatures += 'resizable=yes';
  window.open(`/spa/crm/static/index.html#${url}`, '', szFeatures);
};

window.openFullWindowHaveBarForCrm = openWindowSingle;
window.openFullWindowHaveBar = openWindow;
// window.openFullWindowForXtable = openWindow;

window.crmOpenPage = function (routeUrl) {
  window.open(`/spa/crm/static/index.html#${routeUrl}`, '_blank');
};

export const getFormInitDatas = (condition = []) => {
  const initdatas = {};
  condition.map((c) => {
    const items = [];
    c.items.map((field, index) => {
      const key = getKey(field);
      if (field.conditionType == 'BROWSER') {
        if (!isEmpty(field.browserConditionParam)) {
          const replacesDatas = field.browserConditionParam.replaceDatas || field.browserConditionParam.valueObj;
          if (!isEmpty(replacesDatas)) {
					  let values = [],
              names = [];
					  replacesDatas.forEach((d) => {
              values.push(d.id);
              names.push(d.lastname || d.name); // 人力按钮特殊处理
					  });
					  initdatas[key] = {
              value: values.join(','),
              valueSpan: names.join(','),
              valueObj: replacesDatas,
					  };
          }
        }
      } else if (field.conditionType == 'DATE') {
        let selectValue = '0',
          startValue = null,
          endValue = null;
        if (!isEmpty(field.options)) selectValue = WeaTools.getSelectDefaultValue(field.options) || selectValue;
        if (!isEmpty(field.value)) {
				  selectValue = field.value[field.domkey[0]];
				  startValue = field.value[field.domkey[1]];
				  endValue = field.value[field.domkey[2]];
        }
        initdatas[key] = {
				  value: [selectValue, startValue, endValue],
        };
      } else if (field.conditionType == 'SELECT') {
        let value = WeaTools.getSelectDefaultValue(field.options);
        if (field.value !== undefined) value = field.value;
        if (value !== undefined) {
          initdatas[key] = {
            value,
				  };
        }
      } else if (field.conditionType == 'SELECT_LINKAGE') {
        const selectValue = WeaTools.getSelectDefaultValue(field.options) || '0';
        initdatas[key] = {
				  value: [selectValue],
        };
      } else if (field.conditionType == 'SCOPE') {
        let sValue = field.startValue || '',
          eValue = field.endValue || '';
        initdatas[key] = {
				  value: [sValue, eValue],
        };
      } else if (field.conditionType == 'RANGEPICKER') {
        let sValue = field.startDate || field[field.domkey[0]] || '',
          eValue = field.endDate || field[field.domkey[1]] || '';
        initdatas[key] = {
				  value: [sValue, eValue],
        };
      } else {
        initdatas[key] = { value: field.value || '' };
      }
    });
	  });
	  return initdatas;
};

export const crmAddFormRules = (condition = []) => {
  condition.map((c) => {
    const items = [];
    // c.items.map((field,index) => {
    // 	 if(field.viewAttr == "3"){
    // 			field.rules ='required|string'// 校验规则
    // 	 }
    // })
    c.items.map((field, index) => {
      if (field.conditionType == 'BROWSER') {
        if (field.browserConditionParam.viewAttr == '3') {
									 field.rules = 'required|string';// 校验规则
        }
      } else if (field.viewAttr == '3') {
        field.rules = 'required|string';// 校验规则
      }
    });
  });
  return condition;
};
// 网址验证
export const isURL = (value) => {
  const strRegex = '(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]';
  const re = new RegExp(strRegex);
	 return !!re.test(value);
};
export const isEmail = (value) => {
  // /const req =  new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
  const req = new RegExp('^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$');
  return !!req.test(value);
};

export const getCustomerNameById = (id) => {
  const crmArr = [];
  WeaTools.callApi('/api/crm/customer/getCrmNamesById', 'GET', { customerIds: id }).then((data) => {
    if (data.status == 'success') { crmArr.push({ id, name: data.datas }); }
  });
  return crmArr;
};
